namespace SimpleWebServices.Web
{
    using System.IO;
    using System.Web;
    using System.Web.Hosting;
    using Moq;
    using Xunit;

    public sealed class HttpContextFactoryFacts
    {
        [Fact]
        public void typedef()
        {
            Assert.True(typeof(HttpContextFactory).IsStatic());
        }

        [Fact]
        public void prop_Current_getNull()
        {
            HttpContextFactory.Mock = null;

            Assert.Null(HttpContextFactory.Current);
        }

        [Fact]
        public void prop_Current_getCurrent()
        {
            HttpContextFactory.Mock = null;
            using (TextWriter writer = new StringWriter())
            {
                HttpWorkerRequest request = new SimpleWorkerRequest("/webapp", @"c:\inetpub\wwwroot\webapp\", "default.aspx", string.Empty, writer);
                HttpContext.Current = new HttpContext(request);

                Assert.NotNull(HttpContextFactory.Current);

                HttpContext.Current = null;
            }
        }

        [Fact]
        public void prop_Current_getMock()
        {
            var mock = new Mock<HttpContextBase>();

            HttpContextFactory.Mock = mock.Object;

            Assert.Same(mock.Object, HttpContextFactory.Current);
        }

        [Fact]
        public void prop_Mock_get()
        {
            HttpContextFactory.Mock = null;

            Assert.Null(HttpContextFactory.Mock);
        }

        [Fact]
        public void prop_Mock_set()
        {
            var mock = new Mock<HttpContextBase>();

            HttpContextFactory.Mock = mock.Object;

            var actual = HttpContextFactory.Mock;

            Assert.Same(mock.Object, actual);
        }

        [Fact]
        public void op_Reset()
        {
            HttpContextFactory.Mock = new Mock<HttpContextBase>().Object;
            HttpContextFactory.Reset();

            Assert.Null(HttpContextFactory.Mock);
        }
    }
}